<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class OrderController extends AdminbaseController{
    
	protected $ad_model;
	
	public function _initialize() {
		parent::_initialize();		
	}
	
	public function index(){
	    $this->display();
	}
	
	public function save($res){
	    $date = I('date');
	    $time = strtotime(trim($date));
	    if(!$date){
	        $this->error('请填写日期','',3);
	    }
	    if($time==strtotime('1970-01-01 08:00:00')){
	        $this->error('日期格式错误，请参考日期格式','',3);
	    }
	    foreach ($res as $k=>$v){
	        $v['goods_id'] = abs(rand(100000,999999));
	        if(strlen($v['order_sn'])>10){
	            $order_sn=$v['order_sn'];
	            $total=$v['total'];
	            $a[$v['order_sn']][] = $v;
	        }else{
	            $a[$order_sn][] = array_merge($v,array('order_sn'=>$order_sn,'total'=>$total));
	        }
	        $a[$order_sn]['mid']= '#'.rand(100000,999999).rand(100000,999999);
	        $a[$order_sn]['banck']=rand(1000,9999);
	        $a[$order_sn]['time_front']=rand(100000,999999);
	        $a[$order_sn]['date']=rand($time+36000,$time+64800);
	        $data['order_sn'] = $order_sn;
	        $data['total'] = $total;
	        $data['ename'] = $v['ename'];
	        $data['num'] = $v['num'];
	        $data['price'] = $v['price'];
	        //M('ticket_data')->data($data)->add();
	    }
	    $name = 'ticket'.time();
	    session('ticket',$name);
	    session('time',$time);
	    F($name,json_encode($a));
	
	}
	
	public function printTicket(){
	    $data=json_decode(F(session('ticket')),true);
	    $this->assign('list',$data);
	    $time=session('time');
	    $time=session('time');
	    $this->assign('time_t',date('H:i:s',$time));
	    $this->assign('time_d',date('Y/m/d',$time));
	    $this->assign('time',date('m/d/y H:i:s',$time));
	    $this->display();
	}
	
	public function upload(){
	    $upload = new \Think\Upload();// 实例化上传类
	    $upload->maxSize   =     3145728 ;// 设置附件上传大小
	    $upload->exts      =     array('xlsx','xls');// 设置附件上传类型
	    $upload->rootPath  =     C("UPLOADPATH"); // 设置附件上传根目录
	    $upload->savePath  =     ''; // 设置附件上传（子）目录
	    // 上传文件
	    $info   =   $upload->upload();
	    if(!$info) {// 上传错误提示错误信息
	        $this->error($upload->getError());
	    }else{// 上传成功   */
	        vendor("Excel.PHPExcel");
	        $file_name=$upload->rootPath.$info['photo']['savepath'].$info['photo']['savename'];
	        //$file_name='./Uploads/2017-02-11/589ee2e6a0f06.xls';
	        //$objReader = \PHPExcel_IOFactory::createReader('Excel5');
	        $objPHPExcel = \PHPExcel_IOFactory::load($file_name,$encode='utf-8');
	        $sheet = $objPHPExcel->getSheet(0);
	        $highestRow = $sheet->getHighestRow(); // 取得总行数
	        $highestColumn = $sheet->getHighestColumn(); // 取得总列数
	        $objdata = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
	        foreach($objdata[1] as $k=>$v){
	            $v = str_replace(array("\r\n", "\r", "\n"),'',$v);
	            switch ($v)
	            {
	                case "英文品名":
	                    $key_ename = $k;
	                    break;
	                case "总价":
	                    $key_total = $k;
	                    break;
	                case "内物数量":
	                    $key_num=$k;
	                    break;
	                case "单价":
	                    $key_price = $k;
	                    break;
	                case "分运单号":
	                    $key_order = $k;
	                    break;
	            }
	        }
	        for($i=2;$i<=$highestRow;$i++)
	        {
	            $data[$i]['order_sn'] = $objdata[$i][$key_order];
	            $data[$i]['total'] = str_replace('$','',$objdata[$i][$key_total]);
	            $data[$i]['ename'] = $objdata[$i][$key_ename];
	            $data[$i]['num'] = $objdata[$i][$key_num];
	            $data[$i]['price'] = str_replace('$','',$objdata[$i][$key_price]);
	        }
	        $this->save($data);
	        //dump($data);
	        $this->success('生成成功！',U('admin/Order/printTicket'));
	    }
	}
	
	public function uploadCode(){
	    $upload = new \Think\Upload();// 实例化上传类
	    $upload->maxSize   =     3145728 ;// 设置附件上传大小
	    $upload->exts      =     array('xlsx','xls');// 设置附件上传类型
	    $upload->rootPath  =     C("UPLOADPATH"); // 设置附件上传根目录
	    $upload->savePath  =     ''; // 设置附件上传（子）目录
	    // 上传文件
	    $info   =   $upload->upload();
	    if(!$info) {// 上传错误提示错误信息
	        $this->error($upload->getError());
	    }else{// 上传成功   */
	        vendor("Excel.PHPExcel");
	        $file_name=$upload->rootPath.$info['photo']['savepath'].$info['photo']['savename'];
	        $objPHPExcel = \PHPExcel_IOFactory::load($file_name,$encode='utf-8');
	        $sheet = $objPHPExcel->getSheet(0);
	        $highestRow = $sheet->getHighestRow(); // 取得总行数
	        $highestColumn = $sheet->getHighestColumn(); // 取得总列数
	        $objdata = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
	        foreach($objdata[1] as $k=>$v){
	            $v = str_replace(array("\r\n", "\r", "\n"),'',$v);
	            switch ($v)
	            {
	                case "运单号":
	                    $key_ename = $k;
	                    break;
	            }
	        }
	        for($i=2;$i<=$highestRow;$i++)
	        {
	            $data[$i]['sn'] = $objdata[$i][$key_ename];
	        }
	        $name = 'code'.time();
	        session('code',$name);
	        F($name,json_encode($data));
	        $this->success('生成成功！',U('admin/Order/code'));
	    }
	}
	
	public function createBarCode()
	{
	    Vendor('Barcode.Service');
	    $bar_code = new \Service(18);
	    $bar_code->drawing(I('sn'));
	}
	
	public function code(){
	    $data=json_decode(F(session('code')),true);
	    $this->assign('list',$data);
	    $this->display();
	}
	
	
	
}